﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HorrorsOfArkham.Game_Application_Layer
{
    public enum ProcessState
    { 
        //Created but not running
        UNINITIALIZED,
        //Removed from process list but not destroyed
        REMOVED,
        //Initialized and running
        RUNNING,
        //Initialized but paused
        PAUSED,
        //Completed succesfully
        SUCCEEDED,
        //Failed to complete
        FAILED,
        //Aborted
        ABORTED
    }

    public class Process
    {
        #region Members

        //Current state of the process
        private ProcessState m_state;

        //Child is a process which starts in the next frame after parent process succeeds
        private Process m_childProcess;

        #endregion

        #region Initialization

        public Process()
        { 
        }

        #endregion

        #region Accessors

        public ProcessState GetState()
        {
            return m_state;
        }

        public bool IsAlive()
        {
            return (m_state == ProcessState.RUNNING || m_state == ProcessState.PAUSED);
        }

        public bool IsDead()
        { 
            return (m_state == ProcessState.SUCCEEDED || m_state == ProcessState.FAILED || m_state == ProcessState.ABORTED);
        }

        public bool IsRemoved()
        {
            return m_state == ProcessState.REMOVED;
        }

        public bool IsPaused()
        {
            return m_state == ProcessState.PAUSED;
        }

        #endregion

        #region Process Functionality

        //Loop related functions

        protected virtual void OnInit()
        { 
            m_state = ProcessState.RUNNING; 
        }

        protected virtual void OnUpdate(long deltaMS)
        { }

        protected virtual void OnSucces()
        { }

        protected virtual void OnFail()
        { }

        protected virtual void OnAbort()
        { }

        #endregion

        #region Pausing and Ending Process

        //Functions for ending the process
        public virtual void Succeed()
        { }

        public virtual void Fail()
        { }

        //Pausing
        public virtual void Pause()
        { }

        public virtual void UnPause()
        { }

        #endregion

        #region Child Methods

        public void AttachChild(Process child)
        {
            m_childProcess = child;
        }

        public Process RemoveChild()
        {
            Process oldChild = m_childProcess;
            m_childProcess = null;

            return oldChild;
        }

        public Process PeekChild()
        {
            return m_childProcess;
        }

        #endregion

        #region Other Methods

        public void SetState(ProcessState newState)
        {
            m_state = newState;
        }

        #endregion
    }
}
